﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using stockfund.src;
using MySQLDriverCS;
using System.Data.Common;
using System.Data.SqlClient;
using System.Net;
namespace stockfund.popform
{
    public partial class frmGetData : Form
    {

        public frmGetData()
        {
            InitializeComponent();
        }
        private MySQLConnection conn;
        public frmGetData(MySQLConnection conn)
        {
            this.conn = conn;
            InitializeComponent();
            bool b1 = true;
            bool b2 = true;
            bool b3 = true;
            //Tools.getHangyeFundFromHexun("")
            string d = DateTime.Now.ToString("yyyy-MM-dd");
            if (Array.IndexOf<string>(Tools.stop_2010, d) != -1)
            {
                MessageBox.Show("今天无数据！");
                this.btn_gethangye.Enabled = false;
                this.btn_getstockrank.Enabled = false;
                this.btn_getstockzcgg.Enabled = false;
            }
            string sql = "select * from stockzcgg where statdate='"+d+"' limit 1";
            MySQLCommand cmd = new MySQLCommand("set names gbk",conn);
            cmd.ExecuteNonQuery();
            cmd = new MySQLCommand(sql,conn);
            DbDataReader read = cmd.ExecuteReader();
            while (read.Read())
            {
                this.btn_getstockzcgg.Enabled = false;
                b1 = false;
            }


            sql = "select * from Stockinrank where statdate='" + d + "' limit 1";
            cmd = new MySQLCommand(sql, conn);
            read = cmd.ExecuteReader();
            while (read.Read())
            {
                this.btn_getstockrank.Enabled = false;
                b2 = false;
            }

            sql = "select * from Hangyefund where statdate='" + d + "' limit 1";
            cmd = new MySQLCommand(sql, conn);
            read = cmd.ExecuteReader();
            while (read.Read())
            {
                this.btn_gethangye.Enabled = false;
                b3 = false;
            }
            if(!b1 && !b2 && !b3)
            {
                MessageBox.Show("现在不需要统计数据");
            }
        }
        private void frmGetData_Load(object sender, EventArgs e)
        {
            
        }
        private void getHangyeFund()
        {
            string tsql = "insert into hangyefund(`rank`,`hangye`,`hasstocks`,`sumfund`,`infund`,`percent`,`statdate`,`atype`)"+
                "values('$rank','$hangye','$hasstocks','$sumfund','$infund','$percent',left(now(),10),'$atype')";
            MySQLCommand cmd = new MySQLCommand("set names gbk", conn);
            cmd.ExecuteNonQuery();
            int pos = 1;
            foreach(string atype in GLOBAL_DATA.HANGTETYPE)
            {
                //this.rtbx_hangyefund.Text += "\n==================================\n";
                string url = GLOBAL_DATA.HANGYEURL + pos;
                //this.rtbx_hangyefund.Text += "\n++++++++++++++++++++++++++++++++++\n" + url;
                int prex = 1;
                if (pos % 2 == 1)
                {
                    prex = -1;
                }
                Hangyefund[]  data = src.Tools.getHangyeFundFromHexun(url);
                foreach (Hangyefund o in data)
                {
                    if (o != null)
                    {
                        string sql = tsql.Replace("$rank", o.Rank.ToString())
                            .Replace("$hangye", o.Hangye)
                            .Replace("$hasstocks", o.Hasstocks.ToString())
                            .Replace("$sumfund", o.Sumfund.ToString())
                            .Replace("$infund", (prex * o.Infund).ToString())
                            .Replace("$percent", o.Percent.ToString())
                            .Replace("$atype", atype);
                        cmd = new MySQLCommand(sql, conn);
                        cmd.ExecuteNonQuery();
                        //this.rtbx_hangyefund.Text += "\n-------------------------------\n" + o;
                    }
                }
                pos++;
            }
        }

        private void getStockinrank()
        {
            string tsql = "insert into stockinrank(`rank`,`code`,`name`,`infund`,`percent`,`statdate`,`atype`)"+
                "values('$rank','$code','$name','$infund','$percent',left(now(),10),'$atype')";
            MySQLCommand cmd = new MySQLCommand("set names gbk", conn);
            cmd.ExecuteNonQuery();
            int pos = 1;
            foreach(string atype in GLOBAL_DATA.STOCKINRANKTYPE)
            {
                //this.rtbx_hangyefund.Text += "\n==================================\n";
                string url = GLOBAL_DATA.STOCKINRANKURL.Replace("$pos",pos.ToString());
                //this.rtbx_hangyefund.Text += "\n++++++++++++++++++++++++++++++++++\n" + url;
                int prex = 1;
                if (pos % 2 == 1)
                {
                    prex = -1;
                }
                Stockinrank[]  data = src.Tools.getStockInRankFromHexun(url);
                foreach (Stockinrank o in data)
                {
                    if (o != null)
                    {
                        string sql = tsql.Replace("$rank", o.Rank.ToString())
                            .Replace("$code", o.Code)
                            .Replace("$name", o.Name)
                            .Replace("$infund", (prex * o.inFund).ToString())
                            .Replace("$percent", o.Percent.ToString())
                            .Replace("$atype", atype);
                        cmd = new MySQLCommand(sql, conn);
                        cmd.ExecuteNonQuery();
                        //this.rtbx_hangyefund.Text += "\n-------------------------------\n" + o;
                    }
                }
                pos++;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.getHangyeFund();
            this.showInfo(sender);
            this.btn_gethangye.Enabled = false;
        }

        private void showInfo(object sender)
        {
            Button btn = sender as Button;
            this.rtbx_hangyefund.Text += btn.Text +"已完成\n\n";
        }
        private void button2_Click(object sender, EventArgs e)
        {
            this.getStockinrank();
            this.showInfo(sender);
            this.btn_getstockrank.Enabled = false;
        }

        private void getStockzcgg()
        {
            string tsql = "insert into stockzcgg(`code`,`name`,`fund`,`statdate`,`atype`,`days`,`qujianbi`,`liutongbi`,`swap`,`range`,`time`)" +
         "values('$code','$name','$fund',left(now(),10),'$atype','$days','$qujianbi','$liutongbi','$swap','$range','$time')";
            MySQLCommand cmd = new MySQLCommand("set names gbk", conn);
            cmd.ExecuteNonQuery();
            
            int index = 0;
            foreach (string atype in GLOBAL_DATA.STOCKZCGGTYPE)
            {
                //this.rtbx_hangyefund.Text += "\n==================================\n";
                string url = GLOBAL_DATA.STOCKZCGGURL[index];
                //this.rtbx_hangyefund.Text += "\n++++++++++++++++++++++++++++++++++\n" + url;
                Stockzcgg[] data = src.Tools.getStockZcggFromHexun(url);
                foreach (Stockzcgg o in data)
                {
                    if (o != null)
                    {
                        string sql = tsql.Replace("$code", o.Code)
                            .Replace("$name", o.Name)
                            .Replace("$fund", (o.Fund).ToString())
                            .Replace("$atype", atype)
                            .Replace("$days", o.Days.ToString())
                            .Replace("$qujianbi", o.Qujb.ToString())
                            .Replace("$liutongbi", o.Liutb.ToString())
                            .Replace("$swap", o.Swap.ToString())
                            .Replace("$range", o.Range.ToString())
                            .Replace("$time", o.Time);
                        cmd = new MySQLCommand(sql, conn);
                        cmd.ExecuteNonQuery();
                        //this.rtbx_hangyefund.Text += "\n-------------------------------\n" + o;
                    }
                }
                index++;
            }
        }
        private void operateZcgg()
        {
            string sql = "select `code`,`name`,`days`,`fund`,`qujianbi`,`liutongbi`,`swap`,`range`,`time`,`statdate`,`atype` from stockzcgg order by id asc";
            MySQLCommand cmd = new MySQLCommand("set names gbk",conn);
            cmd.ExecuteNonQuery();
            cmd = new MySQLCommand("delete from stockzcggok", conn);
            cmd.ExecuteNonQuery();
            cmd = new MySQLCommand(sql, conn);
            DbDataReader read = cmd.ExecuteReader();
            Stockzcggok zcgg;
            HashMap hm = new HashMap();
            int old = 0;
            while (read.Read())
            {
                zcgg = new Stockzcggok();
                string code = read["code"].ToString();
                string name = read["name"].ToString();
                int days = Int32.Parse(read["days"].ToString());
                double fund = double.Parse(read["fund"].ToString());
                double qujianbi = double.Parse(read["qujianbi"].ToString());
                double liutongbi = double.Parse(read["liutongbi"].ToString());
                double swap = double.Parse(read["swap"].ToString());
                double range = double.Parse(read["range"].ToString());
                string time = read["time"].ToString();
                DateTime statdate = DateTime.Parse(read["statdate"].ToString());
                string atype = read["atype"].ToString();
                old++;

                int y, d, m;
                string st, ed;
                DateTime start, end;
                st = time.Split('-')[0].Trim();
                ed = time.Split('-')[1].Trim();
                y = Int32.Parse(st.Substring(0, 4));
                m = Int32.Parse(st.Substring(4, 2));
                d = Int32.Parse(st.Substring(6, 2));
                start = new DateTime(y, m, d);
                y = Int32.Parse(ed.Substring(0, 4));
                m = Int32.Parse(ed.Substring(4, 2));
                d = Int32.Parse(ed.Substring(6, 2));
                end = new DateTime(y, m, d);


                zcgg.Code = code;
                zcgg.Name = name;
                zcgg.Fund = fund;
                zcgg.Qujb = qujianbi;
                zcgg.Liutb = liutongbi;
                zcgg.Days = days;
                zcgg.Swap = swap;
                zcgg.Time = time;
                zcgg.Range = range;
                zcgg.Statdate = statdate;
                zcgg.aType = atype;
                zcgg.End = end;
                zcgg.Start = start;

                string key = start + code + atype;
                hm.Add(key, zcgg);
            }
            sql = "insert into stockzcggok (`code`,`name`,`days`,`fund`,`qujianbi`,`liutongbi`,`swap`,`range`,`time`,`statdate`,`atype`,`starttime`,`endtime`) values('$code','$name','$days','$fund','$qujianbi','$liutongbi','$swap','$range','$time','$statdate','$atype','$starttime','$endtime')";
            foreach (string key in hm.Keys)
            {
                Stockzcggok o = (Stockzcggok)hm[key];
                string tsql = sql.Replace("$code", o.Code)
                        .Replace("$name", o.Name)
                        .Replace("$fund", (o.Fund).ToString())
                        .Replace("$atype", o.aType)
                        .Replace("$days", o.Days.ToString())
                        .Replace("$qujianbi", o.Qujb.ToString())
                        .Replace("$liutongbi", o.Liutb.ToString())
                        .Replace("$swap", o.Swap.ToString())
                        .Replace("$range", o.Range.ToString())
                        .Replace("$statdate", o.Statdate.ToString("yyyy-MM-dd"))
                        .Replace("$time", o.Time)
                        .Replace("$endtime", o.End.ToString("yyyy-MM-dd"))
                        .Replace("$starttime", o.Start.ToString("yyyy-MM-dd"));
                cmd = new MySQLCommand(tsql, conn);
                cmd.ExecuteNonQuery();
                
            }
            this.rtbx_hangyefund.Text += "\n处理完成\n\t旧数据：" + old.ToString() + "条\n\t数据：" + hm.Count + "条";

        }
        private void button3_Click(object sender, EventArgs e)
        {
            this.getStockzcgg();
            this.showInfo(sender);
            this.operateZcgg();
            this.btn_getstockzcgg.Enabled = false;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //this.operateZcgg();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            operateZcgg();
        }
    }
}
